Reflecting on Jest and RTL

Nuh

Lessons

  1. What do you prioritise, integration tests or unit tests? In an app that is not entirely and purely a collection of components, you will usually need to prioritise between integration tests i.e. the minimum the client expects from (all features working correctly) and unit tests ensuring your components are robust and resilient.

  2. What do you write first? Write test descriptions and “goals” of your components/pages first using the functional specification / user stories.

    describe("Component/page you are testing", () => {
      it("should achieve this business condition", () => {})
    
      it("should achieve this business condition 2", () => {})
    
      it("should achieve this business condition 3", () => {})
    })

    Once you have written all your test blocks and descriptions, you can then return to tackle the assertions for each, one by one.

    Why? Here are the painful lessons I learnt from this:

You’ll get testing fatigue when things you tested start repeating again and you will ask yourself why that earlier testing from a different angle wouldn’t be sufficient. Unlike writing the actual code where you have to avoid repetition, testing grants you the freedom to duplicate and repeat similar looking tests across suites. You’ll get anxiety over whether you covered everything. The more you write, the more you start wondering ‘what haven’t I covered yet?’ No doubt, the anxiety will only increase once you start switching between spec (user stories), code and test environments. You will also get anxious about when you’ll be done testing! You don’t have an end in sight when you are just going through component by component and forcing your brain to go through the cycle of

find business conditions --> write descriptively --> review the implementation --> write test blocks with the right props for the component --> write assertions

You’ll burn time switching between contexts and trying to fetch details for them, and brainstorming what could be edge cases.

  1. While repetition is tolerated in testing, how do you make sure you don’t burn too much time repeating same old items of code?

    Make effective use of beforeEach, beforeAll, afterEach, afterAll along with functions like cleanup and a base prop you can copy, extend and reuse across all your test blocks will go a long way.

    This will make it easier to read, continue writing tests and to update efficiently as well.

Nuh © 2024